﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Maticsoft.Common;
using System.Text;
using Newtonsoft.Json;
using System.Web.SessionState;

namespace jmwqz.ajax
{
    /// <summary>
    /// editaddr 的摘要说明
    /// </summary>
    public class editaddr : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string _action = MyCommFun.QueryString("myact");
            if (_action == "getProvince")
            {
                #region 选择省份
                Maticsoft.BLL.pre_common_district areaBll = new Maticsoft.BLL.pre_common_district();
                IList<Maticsoft.Model.pre_common_district> disList = areaBll.GetModelList(" level=1 ");
                Maticsoft.Model.pre_common_district dis;
                StringBuilder jsonStr = new StringBuilder("{\"errCode\":0,\"retCode\":0,\"msgType\":0,\"errMsg\":\"\",\"data\":[");
                for (int i = 0; i < disList.Count; i++)
                {
                    dis = new Maticsoft.Model.pre_common_district();
                    if (i != disList.Count - 1)
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"},");
                    }
                    else
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"}");
                    }
                }
                jsonStr.Append("]}");
                context.Response.Write(jsonStr);
                #endregion
            }
            else if (_action == "getCity")
            {
                #region 选择省份，改变城市列表
                int privice = MyCommFun.RequestInt("areaid");
                Maticsoft.BLL.pre_common_district areaBll = new Maticsoft.BLL.pre_common_district();
                IList<Maticsoft.Model.pre_common_district> disList = areaBll.GetModelList("upid=" + privice + " and level=2");
                Maticsoft.Model.pre_common_district dis;
                StringBuilder jsonStr = new StringBuilder("{\"errCode\":0,\"retCode\":0,\"msgType\":0,\"errMsg\":\"\",\"data\":[");
                for (int i = 0; i < disList.Count; i++)
                {
                    dis = new Maticsoft.Model.pre_common_district();
                    if (i != disList.Count - 1)
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"},");
                    }
                    else
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"}");
                    }
                }
                jsonStr.Append("]}");
                context.Response.Write(jsonStr);
                #endregion

            }
            else if (_action == "getArea")
            {
                #region 选择城市，改变区域列表
                int ctid = MyCommFun.RequestInt("areaid");
                Maticsoft.BLL.pre_common_district areaBll = new Maticsoft.BLL.pre_common_district();
                IList<Maticsoft.Model.pre_common_district> disList = areaBll.GetModelList("upid=" + ctid + " and level=3");
                Maticsoft.Model.pre_common_district dis;
                StringBuilder jsonStr = new StringBuilder("{\"errCode\":0,\"retCode\":0,\"msgType\":0,\"errMsg\":\"\",\"data\":[");
                for (int i = 0; i < disList.Count; i++)
                {
                    dis = new Maticsoft.Model.pre_common_district();
                    if (i != disList.Count - 1)
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"},");
                    }
                    else
                    {
                        jsonStr.Append("{\"id\":" + disList[i].id + ",\"name\":\"" + disList[i].name + "\"}");
                    }
                }
                jsonStr.Append("]}");
                context.Response.Write(jsonStr);
                #endregion

            }
            else if (_action == "addcontent")
            {
                Maticsoft.BLL.wx_shop_user_addr baddr = new Maticsoft.BLL.wx_shop_user_addr();
                Maticsoft.Model.wx_shop_user_addr maddr = new Maticsoft.Model.wx_shop_user_addr();
                maddr.addrDetail = MyCommFun.RequestParam("address");
                maddr.province = MyCommFun.RequestParam("pid");
                maddr.city = MyCommFun.RequestParam("cid");
                maddr.area = MyCommFun.RequestParam("rid");
                maddr.tel = MyCommFun.RequestParam("phone");
                maddr.contractPerson = MyCommFun.RequestParam("name");
                maddr.createDate = DateTime.Now;
                maddr.isDefault = "1";
                maddr.jiedao = "";
                maddr.openid = "";
                maddr.wid = Convert.ToInt32(context.Session["userid"].ToString());

                baddr.Add(maddr);
                context.Response.Write("0");
            }
            else if (_action == "editcontent")
            {
                int id = MyCommFun.RequestInt("id");
                Maticsoft.BLL.wx_shop_user_addr baddr = new Maticsoft.BLL.wx_shop_user_addr();
                Maticsoft.Model.wx_shop_user_addr maddr = baddr.GetModel(id);
                if (maddr != null)
                {
                    maddr.addrDetail = MyCommFun.RequestParam("address");
                    maddr.province = MyCommFun.RequestParam("pid");
                    maddr.city = MyCommFun.RequestParam("cid");
                    maddr.area = MyCommFun.RequestParam("rid");
                    maddr.tel = MyCommFun.RequestParam("phone");
                    maddr.contractPerson = MyCommFun.RequestParam("name");
                    baddr.Update(maddr);
                    context.Response.Write("0");
                }
            }

            else if (_action == "getcontent")
            {
                int id = MyCommFun.RequestInt("id");
                Maticsoft.BLL.wx_shop_user_addr baddr = new Maticsoft.BLL.wx_shop_user_addr();
                Maticsoft.Model.wx_shop_user_addr maddr = baddr.GetModel(id);
                if (maddr != null)
                {
                    context.Response.Write(JsonConvert.SerializeObject(maddr));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}